package com.shiguo365.market.display.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;

import com.shiguo365.market.R;
import com.shiguo365.market.display.view.IRegisterView;
import com.shiguo365.market.presenter.implement.RegisterPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity implements IRegisterView {

    RegisterPresenter registerPresenter;

    @BindView(R.id.editTextRegUserName) EditText edt_RegUserName;
    @BindView(R.id.editTextRegSMSCode) EditText edt_RegSMSCode;
    @BindView(R.id.editTextRegUserPassword) EditText edt_UserPassword;
    @BindView(R.id.editTextRegUserPasswordAgain) EditText edt_RegUserPasswordAgain;
    @BindView(R.id.editTextRegShopName) EditText edt_RegShopName;
    @BindView(R.id.editTextRegShopPerson) EditText edt_RegShopPerson;
    @BindView(R.id.spinnerRegShopProvince) Spinner edt_RegShopProvince;
    @BindView(R.id.spinnerRegShopCity) Spinner edt_RegShopCity;
    @BindView(R.id.editTextRegShopAddress) EditText edt_RegShopAddress;
    @BindView(R.id.progressBarregister ) ProgressBar pbar_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        this.setTitle("用户注册");
        ButterKnife.bind(this); //ButterKnife初始化
        registerPresenter = new RegisterPresenter(this,this);
        pbar_register.bringToFront();
        pbar_register.setVisibility(View.GONE);
    }

    @OnClick(R.id.buttonReg)
    public void register(){
        String username=edt_RegUserName.getText().toString();
        String code=edt_RegUserName.getText().toString();
        String pwd=edt_RegUserName.getText().toString();
        String pwd_cfg=edt_RegUserName.getText().toString();
        String shopName=edt_RegUserName.getText().toString();
        String realName=edt_RegUserName.getText().toString();
        String province=edt_RegUserName.getText().toString();
        String city=edt_RegUserName.getText().toString();
        String street=edt_RegUserName.getText().toString();

        if("".equals(username)){
            this.ShowRegisterMessages("手机号不能为空");
            return;
        }
        if("".equals(code)){
            this.ShowRegisterMessages("验证码不能为空");
            return;
        }
        if("".equals(pwd)){
            this.ShowRegisterMessages("登录密码不能为空");
            return;
        }
        if("".equals(pwd_cfg)){
            this.ShowRegisterMessages("确认登录密码不能为空");
            return;
        }
        if(!pwd.equals(pwd_cfg)){
            this.ShowRegisterMessages("登录密码和确认登录密码不一致");
            return;
        }
        if("".equals(shopName)){
            this.ShowRegisterMessages("公司名称不能为空");
            return;
        }
        if("".equals(realName)){
            this.ShowRegisterMessages("您的姓名不能为空");
            return;
        }
        if("".equals(province)){
            this.ShowRegisterMessages("省份不能为空");
            return;
        }
        if("".equals(city)){
            this.ShowRegisterMessages("城市不能为空");
            return;
        }
        if("".equals(street)){
            this.ShowRegisterMessages("公司地址不能为空");
            return;
        }
        pbar_register.setVisibility(View.VISIBLE);
        registerPresenter.register(username,code,pwd,shopName,realName,province,city,street);
    }

    @OnClick(R.id.buttonSendSMSCode)
    public void smscheck(){
        String mobile=edt_RegUserName.getText().toString();

        if("".equals(mobile)){
            this.ShowRegisterMessages("手机号不能为空");
            return;
        }
        pbar_register.setVisibility(View.VISIBLE);
        registerPresenter.smscheck(mobile);
    }

    //显示消息
    @Override
    public void ShowRegisterMessages(String msg) {
        Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
        pbar_register.setVisibility(View.GONE);
    }

    //注册成功后进入登录页面
    @Override
    public void RegisterSuccess() {
        pbar_register.setVisibility(View.GONE);
        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
